package com.youdoneed.community.group.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.group.entity.Forumuserapplyjoingrouprecord;
import com.youdoneed.community.group.service.ForumuserapplyjoingrouprecordService;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.model.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 申请加入小组模块控制器
 * @author lid
 * @date 2017.6.25
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/applyJoinGroup" , method = {RequestMethod.GET,RequestMethod.POST})
@Api(value = "申请模块控制器", description = "申请模块控制器")
public class ForumuserapplyjoingrouprecordController {
	
	private final Logger logger = LoggerFactory.getLogger(ForumuserapplyjoingrouprecordController.class);
	
	@Autowired
	ForumuserapplyjoingrouprecordService forumuserapplyjoingrouprecordService;
	
	@Autowired
    UserFeignClient userFeignClient;
	/**
	 * 申请加入小组
	 * @param userId
	 * @param forumGroupId
	 * @return ResultMessage
	 * @author lid
	 * @date 2017.6.25
	 */
	@RequestMapping(value = "/applyJoinGroup", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "申请加入小组", value = "申请加入小组")
	public ResultMessage<Integer> applyJoinGroup(@ApiParam("用户id")@RequestParam String userId,
										   @ApiParam("小组id")@RequestParam String forumGroupId){
		logger.info("applyJoinGroup");
		int result = forumuserapplyjoingrouprecordService.saveApplyJoinGroupRecord(userId, forumGroupId);
		if(result==0){
			return ResultMessage.newSuccess("提交申请成功");
		}
		return ResultMessage.newSuccess("加入小组成功"); 
	}
	
	/**
	 * 处理申请
	 * @param userApplyJoinGroupRecordId
	 * @param applyStatus
	 * @author lixs
	 * @Date 2017.7.3
	 */
	@RequestMapping(value = "/manageJoinGroupApplication", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "处理申请", value = "处理申请")
	public ResultMessage<Integer> manageJoinGroupApplication(@ApiParam("群主id")@RequestParam String leaderUserId,
															 @ApiParam("用户申请记录id")@RequestParam String userApplyJoinGroupRecordId,
															 @ApiParam("申请状态 申请中（10100），同意加入（10101），拒绝加入（10102）")@RequestParam Integer applyStatus){
		logger.info("处理申请");
		int result = forumuserapplyjoingrouprecordService.manageJoinGroupApplication(leaderUserId,userApplyJoinGroupRecordId, applyStatus);
		if(result==0){
			return ResultMessage.newSuccess("你的加群申请已被拒绝");
		}
		return ResultMessage.newSuccess("加入小组成功");
	} 
	
	/**
	 * 获取申请记录
	 * @param forumgroupId
	 * @param limit
	 * @param offset
	 * @author lixs
	 * @Date 2017.7.8
	 */
	@RequestMapping(value = "/getApplicationByGroupId", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "获取申请列表", value = "获取申请列表")
	public ResultMessage<List<Forumuserapplyjoingrouprecord>> getApplicationByGroupId(@ApiParam("小组id")@RequestParam String forumgroupId,
																					   Integer limit,Integer offset){
		logger.info("获取申请列表");
		List<Forumuserapplyjoingrouprecord> listRecords = forumuserapplyjoingrouprecordService.getApplicationByGroupId(forumgroupId, offset, limit);
		if(listRecords==null){
			return ResultMessage.newFailure("获取申请人列表失败");
		}
		
		for(int index = 0 ; index < listRecords.size(); index++){
			User user = userFeignClient.getUser(listRecords.get(index).getUserId()).getData();
			if(null != user){
				listRecords.get(index).setUserHeadUrl(user.getImageUrl());
				listRecords.get(index).setUserNickName(user.getName());
			}
		}
		
		return ResultMessage.newSuccess("获取申请请列表成功").setData(listRecords);
	}
}	
